package pres.wchen.bussiness.web.controller;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import pres.wchen.bussiness.cor.http.HttpClientManager;
import pres.wchen.bussiness.cor.model.BaseModel;
import pres.wchen.bussiness.cor.performance.AsyncManager;
import pres.wchen.bussiness.cor.svnkit.SvnkitManager;
import pres.wchen.bussiness.service.BusInfoService;
import pres.wchen.bussiness.vo.BusInfoVO;
import pres.wchen.bussiness.vo.BusProjectVersionVO;
import pres.wchen.bussiness.service.BusProjectVersionService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import pres.wchen.bussiness.web.BaseController;

/**
 * @作者： wchen
 * @描述： 商户项目发布情况表管理
 * @创建时间： 2018/02/18 00:55:22
 * @版本：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息：
 */
@RestController
@RequestMapping("/bus/project")
public class BusProjectVersionController extends BaseController {
	@Autowired
	private BusProjectVersionService busProjectVersionService;

	@Autowired
	private BusInfoService busInfoService;

	@Autowired
	private AsyncManager asyncManager;


	/**
     *  @描述: 商户项目发布情况表新增
     *  @参数： [busProjectVersionVO]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/18
     *  @修改人和其它信息:
     */
	@PostMapping(value = "/add")
	public Map insert(BusProjectVersionVO busProjectVersionVO) {
		Long id = busProjectVersionService.insert(busProjectVersionVO);
		return super.dealInsertResult(id);
	}


	/**
     *  @描述: 商户项目发布情况表删除
     *  @参数： [id]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/18
     *  @修改人和其它信息:
     */
	@DeleteMapping(value = "/delete/{id}")
	public Map delete(@PathVariable("id") Long id) {
		Integer result = busProjectVersionService.delete(id);
		return super.dealDeleteResult(result);
	}


	/**
     *  @描述: 商户项目发布情况表修改
     *  @参数： [busProjectVersionVO]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/18
     *  @修改人和其它信息:
     */
	@PutMapping(value = "/modify/")
	public Map modify(BusProjectVersionVO busProjectVersionVO) {
		Integer result = busProjectVersionService.update(busProjectVersionVO);
		return super.dealUpdateResult(result);
	}


	/**
	 *  @描述: 商户项目版本变更(调用其他接口)
	 *  @参数： [busProjectVersionVO]
	 *  @返回值： java.util.Map
	 *  @创建人: wchen
	 *  @创建时间: 2018/2/18
	 *  @修改人和其它信息:
	 */
	@GetMapping(value = "/updateVersion/")
	public Map updateVersion(HttpServletRequest request) {
		String id = request.getParameter("versionId");
		String busId = request.getParameter("busId");
		String versionNo = request.getParameter("versionNo");
		String updateType = request.getParameter("updateType");
		Map conditionMap = new HashMap();
		conditionMap.put("id", id);

		String requestUrl ="";
		BusProjectVersionVO versionVO = busProjectVersionService.info(conditionMap);
		if(versionVO != null){
			if(versionVO.getStatus().equals("2")){
				return super.commonFailResult("该项目正在更新版本ing...");
			}else{

				//查询服务器地址
				conditionMap.put("id", busId);
				BusInfoVO busInfoVO = busInfoService.info(conditionMap);
				if(busInfoVO == null){
					return super.commonFailResult("服务器地址未配置！");
				}
				requestUrl = "http://" + busInfoVO.getServerIp() + ":8552/version/updateVersion";
//				requestUrl = "http://" + "localhost" + ":8552/version/updateVersion";
				//项目状态至为更新中
				versionVO = new BusProjectVersionVO();
				versionVO.setId(new Long(id));
				versionVO.setStatus("2");
				busProjectVersionService.update(versionVO);
			}
		}

		//调用接口
		Map paramMap = new HashMap();
		paramMap.put("version", versionNo);
		paramMap.put("updateType", updateType);
		paramMap.put("id", id);
		asyncManager.doService(requestUrl, paramMap);

		//版本状态更新
		versionVO = new BusProjectVersionVO();
		versionVO.setId(new Long(id));
		versionVO.setStatus("2");
		Integer result = busProjectVersionService.update(versionVO);
		return super.dealUpdateResult(result);
	}




	/**
     *  @描述: 商户项目发布情况表详情
     *  @参数： [id]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/18
     *  @修改人和其它信息:
     */
	@GetMapping(value = "/info/{busId}")
	public ModelAndView info(@PathVariable("busId") Long busId) throws Exception {
	    Map conditionMap = new HashMap();
        conditionMap.put("busId", busId);
		List<BusProjectVersionVO> busProjectVersionList = busProjectVersionService.list(conditionMap).getList();
		List dataList = new ArrayList();

		for(BusProjectVersionVO projectVersionVO : busProjectVersionList){
			String svnURl = getSvnPath(projectVersionVO.getProjectType());
			String versionNO = projectVersionVO.getVersion();
			List<Map> historyList = SvnkitManager.doSvnHistory(0, -1, svnURl);
			Map dataMap = new HashMap();
			for(Map svnMap : historyList){
				if(versionNO == null){
					svnMap.put("flag", 0);
					continue;
				}
				if(versionNO.equals(svnMap.get("revision").toString())){
					svnMap.put("flag", 1);
				}else{
					svnMap.put("flag", 0);
				}
			}
			dataMap.put("historyList", historyList);
			dataMap.put("enableStatus", projectVersionVO.getStatus());
			dataMap.put("id", projectVersionVO.getId());
			dataMap.put("projectType", projectVersionVO.getProjectType());
			dataList.add(dataMap);
		}

		//封装结果
		BaseModel baseModel = new BaseModel();
		baseModel.setList(dataList);
		baseModel.setTotalPageSize(new Long(dataList.size()));
		//查询商户名
		Map remarkMap = new HashMap();
		remarkMap.put("id", busId);
		BusInfoVO busInfoVO = busInfoService.info(remarkMap);
		remarkMap.clear();
		remarkMap.put("busId", busInfoVO.getId());
		remarkMap.put("busName", busInfoVO.getBusName());

		baseModel.setRemark(remarkMap);
		return super.dealListResult("/bus/project/maintain_modify", baseModel);
	}


	/**
	 *
	 * @描述： 获取相关路径
	 * @名称： getOutputPath
	 * @入参： [operateType]
	 * @出参： void
	 * @修改信息： 创建：wchen 2018/2/5
	 */
	private String getSvnPath(String operateType){

		if(operateType.equals("admin")){
			return SvnkitManager.ADMIN_URL;
		}else if(operateType.equals("client")){
			return SvnkitManager.CLIENT_URL;
		}else if(operateType.equals("sys")){
			return SvnkitManager.SERVICE_SYS_URL;
		}else if(operateType.equals("mall")){
			return SvnkitManager.SERVICE_MALL_URL;
		}else if(operateType.equals("website")){
			return SvnkitManager.SERVICE_WEBSITE_URL;
		}else{
			return SvnkitManager.TIMER_URL;
		}
	}


	/**
     *  @描述: 商户项目发布情况表分页列表
     *  @参数： [request]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/18
     *  @修改人和其它信息:
     */
	@GetMapping(value = "/pageList")
	public ModelAndView pageList(HttpServletRequest request) {
		Object pageSize = request.getParameter("pageSize");
		Object pageNo = request.getParameter("pageNo");
		Object busName = request.getParameter("busName");
		Object pageFlag = request.getParameter("pageFlag");
		Map conditionMap = new HashMap();
		conditionMap.put("pageSize", pageSize);
		conditionMap.put("pageNo", pageNo);
		conditionMap.put("busName", busName);
		conditionMap.put("pageFlag", pageFlag);


		BaseModel baseModel = busProjectVersionService.busProjectList(conditionMap);
		//结果处理
		List<Map> resultList = baseModel.getList();
		if(resultList.size() > 0){
			for(Map versionMap : resultList){
				List<BusProjectVersionVO> contentList = (List<BusProjectVersionVO>) versionMap.remove("contentList");
				StringBuffer sbf = new StringBuffer();
				for(BusProjectVersionVO busProjectVersionVO : contentList){
					String projectType = busProjectVersionVO.getProjectType();
					String status = busProjectVersionVO.getStatus();
					sbf.append(projectType).append(":");
					if(status.equals("0")){
						sbf.append("未初始化！");
					}else if(status.equals("1")){
						sbf.append("更新成功！");
					}else if(status.equals("2")){
						sbf.append("更新中！");
					}else if(status.equals("3")){
						sbf.append("更新失败！").append(busProjectVersionVO.getRemark());
					}
				}
				versionMap.put("remark", sbf.toString());
			}
		}

		return super.dealListResult("/bus/project/maintain", baseModel);
	}

}
